package com.fang.algorithm.zuo.string;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 打印字符串的所有子串
 */
public class Test02_PrintStrAllChild {

    public static void main(String[] args) {
        String str = "abc";
        List<String> ans = printAllChild(str);
        System.out.println(ans);
    }

    private static List<String> printAllChild(String str) {
        List<String> ans = new ArrayList<>();
        if (null == str || str.length() < 1) {
            return ans;
        }
        char[] arr = str.toCharArray();
        printProcess(arr, 0, ans);
        return ans;
    }

    private static void printProcess(char[] arr, int index, List<String> ans) {
        if (arr.length == index) {
            ans.add(Arrays.toString(arr));
            return;
        }
        for (int i = index; i < arr.length; i++) {
            swapValue(arr, i, index);
            printProcess(arr, index + 1, ans);
            // 递归恢复现场向上
            swapValue(arr, i, index);
        }
    }

    private static void swapValue (char[] arr, int i, int j) {
        if (i == j) {
            return;
        }
        char temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
